{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 11.3 \u521b\u5efaUDP\u670d\u52a1\u5668\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5b9e\u73b0\u4e00\u4e2a\u57fa\u4e8eUDP\u534f\u8bae\u7684\u670d\u52a1\u5668\u6765\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8ddfTCP\u4e00\u6837\uff0cUDP\u670d\u52a1\u5668\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 socketserver \u5e93\u5f88\u5bb9\u6613\u7684\u88ab\u521b\u5efa\u3002\n\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u65f6\u95f4\u670d\u52a1\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from socketserver import BaseRequestHandler, UDPServer\nimport time\n\nclass TimeHandler(BaseRequestHandler):\n    def handle(self):\n        print('Got connection from', self.client_address)\n        # Get message and client socket\n        msg, sock = self.request\n        resp = time.ctime()\n        sock.sendto(resp.encode('ascii'), self.client_address)\n\nif __name__ == '__main__':\n    serv = UDPServer(('', 20000), TimeHandler)\n    serv.serve_forever()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8ddf\u4e4b\u524d\u4e00\u6837\uff0c\u4f60\u5148\u5b9a\u4e49\u4e00\u4e2a\u5b9e\u73b0 handle() \u7279\u6b8a\u65b9\u6cd5\u7684\u7c7b\uff0c\u4e3a\u5ba2\u6237\u7aef\u8fde\u63a5\u670d\u52a1\u3002\n\u8fd9\u4e2a\u7c7b\u7684 request \u5c5e\u6027\u662f\u4e00\u4e2a\u5305\u542b\u4e86\u6570\u636e\u62a5\u548c\u5e95\u5c42socket\u5bf9\u8c61\u7684\u5143\u7ec4\u3002client_address \u5305\u542b\u4e86\u5ba2\u6237\u7aef\u5730\u5740\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6211\u4eec\u6765\u6d4b\u8bd5\u4e0b\u8fd9\u4e2a\u670d\u52a1\u5668\uff0c\u9996\u5148\u8fd0\u884c\u5b83\uff0c\u7136\u540e\u6253\u5f00\u53e6\u5916\u4e00\u4e2aPython\u8fdb\u7a0b\u5411\u670d\u52a1\u5668\u53d1\u9001\u6d88\u606f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from socket import socket, AF_INET, SOCK_DGRAM\ns = socket(AF_INET, SOCK_DGRAM)\ns.sendto(b'', ('localhost', 20000))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.recvfrom(8192)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u5178\u578b\u7684UDP\u670d\u52a1\u5668\u63a5\u6536\u5230\u8fbe\u7684\u6570\u636e\u62a5(\u6d88\u606f)\u548c\u5ba2\u6237\u7aef\u5730\u5740\u3002\u5982\u679c\u670d\u52a1\u5668\u9700\u8981\u505a\u5e94\u7b54\uff0c\n\u5b83\u8981\u7ed9\u5ba2\u6237\u7aef\u56de\u53d1\u4e00\u4e2a\u6570\u636e\u62a5\u3002\u5bf9\u4e8e\u6570\u636e\u62a5\u7684\u4f20\u9001\uff0c\n\u4f60\u5e94\u8be5\u4f7f\u7528socket\u7684 sendto() \u548c recvfrom() \u65b9\u6cd5\u3002\n\u5c3d\u7ba1\u4f20\u7edf\u7684 send() \u548c recv() \u4e5f\u53ef\u4ee5\u8fbe\u5230\u540c\u6837\u7684\u6548\u679c\uff0c\n\u4f46\u662f\u524d\u9762\u7684\u4e24\u4e2a\u65b9\u6cd5\u5bf9\u4e8eUDP\u8fde\u63a5\u800c\u8a00\u66f4\u666e\u904d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7531\u4e8e\u6ca1\u6709\u5e95\u5c42\u7684\u8fde\u63a5\uff0cUPD\u670d\u52a1\u5668\u76f8\u5bf9\u4e8eTCP\u670d\u52a1\u5668\u6765\u8bb2\u5b9e\u73b0\u8d77\u6765\u66f4\u52a0\u7b80\u5355\u3002\n\u4e0d\u8fc7\uff0cUDP\u5929\u751f\u662f\u4e0d\u53ef\u9760\u7684\uff08\u56e0\u4e3a\u901a\u4fe1\u6ca1\u6709\u5efa\u7acb\u8fde\u63a5\uff0c\u6d88\u606f\u53ef\u80fd\u4e22\u5931\uff09\u3002\n\u56e0\u6b64\u9700\u8981\u7531\u4f60\u81ea\u5df1\u6765\u51b3\u5b9a\u8be5\u600e\u6837\u5904\u7406\u4e22\u5931\u6d88\u606f\u7684\u60c5\u51b5\u3002\u8fd9\u4e2a\u5df2\u7ecf\u4e0d\u5728\u672c\u4e66\u8ba8\u8bba\u8303\u56f4\u5185\u4e86\uff0c\n\u4e0d\u8fc7\u901a\u5e38\u6765\u8bf4\uff0c\u5982\u679c\u53ef\u9760\u6027\u5bf9\u4e8e\u4f60\u7a0b\u5e8f\u5f88\u91cd\u8981\uff0c\u4f60\u9700\u8981\u501f\u52a9\u4e8e\u5e8f\u5217\u53f7\u3001\u91cd\u8bd5\u3001\u8d85\u65f6\u4ee5\u53ca\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u6765\u4fdd\u8bc1\u3002\nUDP\u901a\u5e38\u88ab\u7528\u5728\u90a3\u4e9b\u5bf9\u4e8e\u53ef\u9760\u4f20\u8f93\u8981\u6c42\u4e0d\u662f\u5f88\u9ad8\u7684\u573a\u5408\u3002\u4f8b\u5982\uff0c\u5728\u5b9e\u65f6\u5e94\u7528\u5982\u591a\u5a92\u4f53\u6d41\u4ee5\u53ca\u6e38\u620f\u9886\u57df\uff0c\n\u65e0\u9700\u8fd4\u56de\u6062\u590d\u4e22\u5931\u7684\u6570\u636e\u5305\uff08\u7a0b\u5e8f\u53ea\u9700\u7b80\u5355\u7684\u5ffd\u7565\u5b83\u5e76\u7ee7\u7eed\u5411\u524d\u8fd0\u884c\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "UDPServer \u7c7b\u662f\u5355\u7ebf\u7a0b\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e00\u6b21\u53ea\u80fd\u4e3a\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u670d\u52a1\u3002\n\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u8fd9\u4e2a\u65e0\u8bba\u662f\u5bf9\u4e8eUDP\u8fd8\u662fTCP\u90fd\u4e0d\u662f\u4ec0\u4e48\u5927\u95ee\u9898\u3002\n\u5982\u679c\u4f60\u60f3\u8981\u5e76\u53d1\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u4f8b\u5316\u4e00\u4e2a ForkingUDPServer \u6216 ThreadingUDPServer \u5bf9\u8c61\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from socketserver import ThreadingUDPServer\n\n   if __name__ == '__main__':\n    serv = ThreadingUDPServer(('',20000), TimeHandler)\n    serv.serve_forever()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u76f4\u63a5\u4f7f\u7528 socket \u6765\u5b9e\u73b0\u4e00\u4e2aUDP\u670d\u52a1\u5668\u4e5f\u4e0d\u96be\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from socket import socket, AF_INET, SOCK_DGRAM\nimport time\n\ndef time_server(address):\n    sock = socket(AF_INET, SOCK_DGRAM)\n    sock.bind(address)\n    while True:\n        msg, addr = sock.recvfrom(8192)\n        print('Got message from', addr)\n        resp = time.ctime()\n        sock.sendto(resp.encode('ascii'), addr)\n\nif __name__ == '__main__':\n    time_server(('', 20000))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}